package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelLike;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TravelLikeMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.service.ITravelLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelLikeServiceImpl implements ITravelLikeService {
    @Autowired
    private TravelLikeMapper travelLikeMapper;
    @Autowired
    private TravelMapper travelMapper;

    @Override
    public TravelLike toByTravelIdAndUserId(Long travelId, User user) {
        Long userId = user.getId();
        TravelLike travelLike = travelLikeMapper.selectByTravelIdAndUserId(travelId, userId);
        //进行判断得到的travelLike对象是不是为空,如果为空
        //执行点赞操作,将数据添加到表中
        //创建travelLike对象
        TravelLike tLike = new TravelLike();
        int countLike;
        Integer id = null;
        if (travelLike == null) {
            //游记id
            Travel travel = travelMapper.selectByPrimaryKey(travelId);
            tLike.setTravel(travel);
            //添加用户
            tLike.setUser(user);
            //添加时间
            tLike.setTime(new Date());
            //添加状态
            tLike.setState(1);
            //            //点赞
            id = travelLikeMapper.insert(tLike);
            //添加点赞的信息条数
            //查詢中閒表中的信息表
            countLike = travelLikeMapper.selectByLikeCount(travelId);
            //添加点赞的信息条数
            countLike = countLike + 1;
            travelLikeMapper.updataTravel(countLike, travelId);
        } else {
            //取消赞操作
            countLike = travelLikeMapper.selectByLikeCount(travelId);
            //将点赞中的信息表中数据减一
            if (countLike > 0) {
                countLike = countLike - 1;
            }
            TravelLike travelLike1 = travelLikeMapper.selectByTravelIdAnduserId(travelId, userId);
            Integer state = travelLike1.getState();
            if (state == 1) {
                tLike.setState(0);
            } else {
                tLike.setState(1);
            }
            travelLikeMapper.updataTravel(countLike, travelId);
            //修改点赞状态
            travelLikeMapper.updateByStrate(travelLike1.getId(), tLike.getState());
        }

        TravelLike travelLik = travelLikeMapper.selectByTravelIdAnduserId(travelId, userId);
        return travelLik;
    }

    /**
     * 点赞回显操作
     *
     * @param travelId
     * @param currentUser
     * @return
     */
    @Override
    public TravelLike selectBytravelId(Long travelId, User currentUser) {
        Long userId = currentUser.getId();
        return travelLikeMapper.selectByTravelIdAnduserId(travelId, userId);
    }

    @Override
    public List<TravelLike> selectAll(int type, Long targetId) {
        return travelLikeMapper.selectAll(type, targetId);
    }

    @Override
    public void changeType(int type, Long targetId) {
        travelLikeMapper.changeType(type, targetId);
    }

}
